// packageJ/projectManage/project/project.js
const app = getApp();
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    incomeName:'',
    point:'',
    usableLove:'',
    frozeLove:'',
    isLoadMore: true,
    page: 1,
    totalPage: 0,
    rewardName:'提成状态',
    balanceName:'结算类型',
    rewardsArr:[
      {
        name:'管理提成',
        status:1
      },
      {
        name:'培育奖',
        status:2
      },
      {
        name:'额外奖励',
        status:3
      }
    ],
    query: {
      awardType:1,
      settledStatus:0,
      settledType:0,
      page:1
    },
    deductList:[
      {
        name:'提成状态',
        status:0
      },
      {
        name:'未结算',
        status:1
      },
      {
        name:'已结算',
        status:2
      },
      {
        name:'已失效',
        status:-1
      }
    ],
    rewardArr:[
      {
        name:'结算类型',
        status:0
      },
      {
        name:'收入',
        status:1
      },
      {
        name:'积分',
        status:2
      },
      {
        name:'可用爱心值',
        status:3
      },
      {
        name:'冻结爱心值',
        status:4
      }
    ],
    rewardStatus:false,
    rewardType:false,
    setting:{},
    statistic:{},
    rewardsList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  royaltiesOn() {
    this.setData({
      rewardStatus:!this.data.rewardStatus,
      rewardType:false
    })
  },
  royaltiesTypeOn() {
    this.setData({
      rewardType:!this.data.rewardType,
      rewardStatus:false
    })
  },
  directlyOn() {
    wx.navigateTo({
      url: '/packageJ/projectManage/directly/directly',
    })
  },
  teamIndex() { //会员信息
    let json = {
      award_type:this.data.query.awardType,
      basic_info:1
    }
    let urlStr = app.getNetAddresss("plugin.manager-team.frontend.controllers.page.index");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        let basic_info = res.basic_info;
        let data = res.data;
        if (res.result == 1) {
          const {member,setting,statistic,manager_team_name} = {...data};
          let rewardArr = this.data.rewardArr;
          rewardArr[1].name = basic_info.lang.income.income_name;
          rewardArr[2].name = basic_info.lang.member_center.credit1 || '积分';
          rewardArr[3].name = basic_info.plugin_setting.love?.usable_love_name;
          rewardArr[4].name = basic_info.plugin_setting.love?.froze_love_name;
          this.setData({
            member,
            setting,
            statistic,
            rewardArr, 
            point:basic_info.lang.member_center.credit1 || '积分',
            usableLove:basic_info.plugin_setting.love?.usable_love_name,
            frozeLove:basic_info.plugin_setting.love?.froze_love_name,
            incomeName:basic_info.lang.income.income_name
          })
          wx.setNavigationBarTitle({title: manager_team_name})
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  deductOn(event) {
    let {item,type} = event.currentTarget.dataset;
    if(type === 'settledStatus') {
      this.data.rewardName = item.name;
    } else {
      this.data.balanceName = item.name;
    }
    this.setData({
      rewardName:this.data.rewardName,
      balanceName:this.data.balanceName,
      rewardStatus:false,
      rewardType:false,
      isLoadMore:true,
      [`query.${type}`]:item.status,
      'query.page': 1
    })
    this.recordsList();
  }, 
  rewardsTab(event) {
    this.setData({
      'query.awardType':event.detail.name,
      'query.page':1,
      isLoadMore:true
    })
    this.teamIndex();
    this.recordsList();
  },
  recordsList() {
    let json = {
      award_type:this.data.query.awardType,
      settled_status: this.data.query.settledStatus,
      settled_type: this.data.query.settledType,
      page:this.data.query.page
    };
    let urlStr = app.getNetAddresss("plugin.manager-team.frontend.modules.income.controllers.records.index");
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        let data = res.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.totalPage = data.page_list.last_page;
          if (!this.data.totalPage) {
            this.data.query.page = 0;
          }
          this.setData({
            rewardsList: data?.page_list?.data || []
          })
        } else {
          this.data.page = this.data.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.query.page >= this.data.totalPage) {
      return;
    } else {
      this.data.query.page = this.data.query.page + 1;
      let json = {
        award_type:this.data.query.awardType,
        settled_status: this.data.query.settledStatus,
        settled_type: this.data.query.settledType,
        page:this.data.query.page
      };
      let urlStr = app.getNetAddresss("plugin.manager-team.frontend.modules.income.controllers.records.index");
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          let list = res?.data.page_list.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              rewardsList : this.data.rewardsList.concat(list),
            })
          } else {
            this.data.query.page = this.data.query.page - 1;
            this.data.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.data.page = 1;
    this.teamIndex();
    this.recordsList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})